﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XinYue.Permission.DAL;
using XinYue.Permission.Mode;

namespace XinYue.Permission.BLL
{
    public class BaseService<T> where T : class
    {
        //当前操作数据库实例
        private BaseRepository<T, PermissionEntities> baseRepository = new BaseRepository<T, PermissionEntities>();

        #region 添加数据
        /// <summary>
        /// 添加单条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Add(T entity)
        {
            return baseRepository.Add(entity);
        }
        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public virtual bool AddRange(List<T> entitys)
        {
            return baseRepository.AddRange(entitys);
        }
        #endregion

        #region 修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            return baseRepository.Update(entity);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            return baseRepository.Delete(entity);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool BatchDelete(List<T> entitys)
        {
            return baseRepository.BatchDelete(entitys);
        }

        /// <summary>
        /// 根据ID批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool BatchDeleteByID(params int[] IDs)
        {
            return baseRepository.BatchDeleteByID(IDs);
        }
        #endregion

        #region 查询数据
        /// <summary>
        /// 查询返回一个集合
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual List<T> QueryList(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.QueryList(lambdaExpression);
        }
        /// <summary>
        /// 查询返回一个对象
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual T Query(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.Query(lambdaExpression);
        }
        /// <summary>
        /// 查询是否存在
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual bool Exists(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.Exists(lambdaExpression);
        }
        #endregion

        #region 查询分页
        public virtual List<T> QueryPageList<S>(int PageIndex, int PageSize, Expression<Func<T, bool>> Wherelambda, Expression<Func<T, S>> OrderBylambda, out int count, bool isAsc = true)
        {
            return baseRepository.QueryPageList(PageIndex, PageSize, Wherelambda, OrderBylambda, out count, isAsc);
        }
        #endregion
    }
}
